<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 14:45
 */

namespace app\admin\controller;


use think\Request;
use think\response\Redirect;
use think\Session;
class Charge extends common
{
    //管理员充值
    public function charge(){
        if(Request::instance()->isPost()){
            $user= input('post.user');
            $type = input('post.type');
            $money = input('post.money/d');
            $remark = input('post.remark');
            $admin = Session::get('admin')['name'];
            if ($type == 1){//id充值
                if(db('user')->where('id',$user)->find()){
                    $usermoney = db('user')->where('id',$user)->value('money');
                    $data['uid'] = $user;
                    $data['type'] = 4;
                    $data['money'] = $money;
                    $data['remark'] = $remark . "(充值者:$admin)";
                    $data['before_money'] = $usermoney;
                    $data['time'] = date('Y-m-d h:i:s',time());
                    $data['number'] = mt_rand(100000,999999);
                    //判断上级充值开关
                    $charge = db('webconfig')->where('id',1)->value('charge');
                    if($charge != 1){
                        $this->error('充值开关已关闭,不能充值!');
                    }
                    if (db('charge')->insert($data)){
                        db('user')->where('id',$user)->setInc('money',$money);
                        $this->success('充值成功!');
                    }else{
                        $this->error('充值错误!');
                    }
                }else{
                    $this->error('所要充值用户不存在,请核实后重试!');
                }
            }elseif ($type == 2){
                if(db('user')->where('name',$user)->find()){
                    $usermoney = db('user')->where('name',$user)->value('money');
                    $data['uid'] = db('user')->where('name',$user)->value('id');
                    $data['type'] = 4;
                    $data['money'] = $money;
                    $data['remark'] = $remark . "(充值者:$admin)";
                    $data['before_money'] = $usermoney;
                    $data['time'] = date('Y-m-d h:i:s',time());
                    $data['number'] = mt_rand(100000,999999);
                    if (db('charge')->insert($data)){
                        db('user')->where('name',$user)->setInc('money',$money);
                        $this->success('充值成功!');
                    }else{
                        $this->error('充值错误!');
                    }
                }else{
                    $this->error('所要充值用户不存在,请核实后重试!');
                }
            }
        }else{
            return $this->fetch();
        }
    }






    public function chargeList(){
        $type = input('get.type/d');
        $start = input('get.start');
        $end = input('get.end');
        $user = input('get.user');
        $where = [];
        if($type){
            $where['type'] = $type;
        }
        if($start && $end){
            if($start > $end){
                $this->error('时间格式错误');
            }else{
                $end =  date("Y-m-d",strtotime("$end+1day"));
                $where['time'] = [['>=',$start],['<=',$end]];
            }

        }
        if($user){
            $data = db('charge')
                ->alias('c')
                ->where($where)
                ->where('u.id|u.name',$user)
                ->field('c.*,u.name,u.gx')
                ->join('user u','c.uid = u.id','LEFT')
                ->select();
        }else{
            $data = db('charge')
                ->alias('c')
                ->where($where)
                ->field('c.*,u.name,u.gx')
                ->join('user u','c.uid = u.id','LEFT')
                ->select();
        }
        $data = $this->guanxi($data);
        $this->assign('data',$data);
        return $this->fetch();
    }

}